package com.sphd.emanu.activity.dis;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.ValueScheme;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.market.Discount;
import com.sphd.emanu.pojo.market.Rules;
import com.sphd.emanu.utils.MyWatcher;
import com.sphd.emanu.utils.PermissionConfig;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 营销模块----折扣率
 *
 * @Author Hu
 * @Time 2017/5/12 -- 16:23.
 * @Email 87683202@qq.com
 */
public class DisActivity extends MVPBaseActivity<DisContract.View, DisPresenter> implements DisContract.View, CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.is_app_order)
    Switch isAppOrder;
    @BindView(R.id.layout_is_app_order)
    LinearLayout layoutIsAppOrder;
    @BindView(R.id.is_before_switch)
    Switch isBeforeSwitch;
    @BindView(R.id.layout_is_before_switch)
    LinearLayout layoutIsBeforeSwitch;
    @BindView(R.id.is_double_dis_switch)
    Switch isDoubleDisSwitch;
    @BindView(R.id.layout_double_dis)
    LinearLayout layoutDoubleDis;
    @BindView(R.id.btn_save)
    TextView btnSave;
    @BindView(R.id.dis_no)
    MaterialEditText disNo;
    @BindView(R.id.dis_no_before)
    MaterialEditText disNoBefore;
    @BindView(R.id.time_before)
    MaterialEditText timeBefore;
    Intent intent;
    Bundle bundle;
    Rules rules;
    Discount discount;
    AndroidNextInputs inputs;
    List<Moudle> moudles;
    boolean isCanDo = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dis);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        isAppOrder.setOnCheckedChangeListener(this);
        isBeforeSwitch.setOnCheckedChangeListener(this);
    }

    private void initView() {
        setTitleBar(toolbar, "折扣率");
        if (isBeforeSwitch.isChecked() && isAppOrder.isChecked()) {
            isDoubleDisSwitch.setEnabled(true);
        } else {
            isDoubleDisSwitch.setEnabled(false);
        }
        inputs = new AndroidNextInputs();
        inputs.add(disNo, StaticScheme.NotBlank().msgOnFail("折扣率不能为空"), StaticScheme
                .Numeric().msgOnFail("请输入数字"), StaticScheme.Digits(), ValueScheme.RangeValue(0.000001, 9.999999)
                .msgOnFail("请输入大于0小10的折扣率"));
        inputs.add(disNoBefore, StaticScheme.NotBlank().msgOnFail("提前下单折扣率不能为空"), StaticScheme
                .Numeric().msgOnFail("请输入数字"), StaticScheme.Digits(), ValueScheme.RangeValue
                (0.000001, 9.999999).msgOnFail("请输入大于0小10的折扣率"));
        inputs.add(timeBefore, StaticScheme.NotBlank().msgOnFail("提前下单时间不能为空"), StaticScheme
                .Numeric().msgOnFail("请输入数字"));
        disNo.addTextChangedListener(new MyWatcher(-1, 1));
        disNoBefore.addTextChangedListener(new MyWatcher(-1, 1));
        intent = getIntent();
        bundle = intent.getExtras();
        moudles = (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo = isCanDo(moudles, PermissionConfig.MENU_MAKETING_4);
        if (!isCanDo) {
            btnSave.setVisibility(View.GONE);
        }
        rules = (Rules) bundle.getSerializable(Constant.RULES);
        discount = rules.getDiscount();
        isAppOrder.setChecked(discount.getEnabled());
        if (isAppOrder.isChecked()) {
            layoutIsAppOrder.setVisibility(View.VISIBLE);
        } else {
            layoutIsAppOrder.setVisibility(View.GONE);
        }
        disNo.setText(String.valueOf(discount.getDiscountRate()/10));
        isBeforeSwitch.setChecked(discount.getAdvanceEnabled());
        if (isBeforeSwitch.isChecked()) {
            layoutIsBeforeSwitch.setVisibility(View.VISIBLE);
        } else {
            layoutIsBeforeSwitch.setVisibility(View.GONE);
        }
        disNoBefore.setText(String.valueOf(discount.getAdvanceRate()/10 ));
        timeBefore.setText(String.valueOf(discount.getAdvanceTime()));
        isDoubleDisSwitch.setChecked(discount.getIsLoop());
        if (isAppOrder.isChecked() && isBeforeSwitch.isChecked()) {
            isDoubleDisSwitch.setEnabled(true);
        } else {
            isDoubleDisSwitch.setEnabled(false);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.is_app_order:
                //是否支持手机端下单打折
                if (isChecked) {
                    layoutIsAppOrder.setVisibility(View.VISIBLE);
                    if (isBeforeSwitch.isChecked()) {
                        isDoubleDisSwitch.setEnabled(true);
                    } else {
                        isDoubleDisSwitch.setEnabled(false);
                    }
                } else {
                    layoutIsAppOrder.setVisibility(View.GONE);
                    isDoubleDisSwitch.setEnabled(false);
                }
                break;
            case R.id.is_before_switch:
                //是否支持手机端下单打折
                if (isChecked) {
                    layoutIsBeforeSwitch.setVisibility(View.VISIBLE);
                    if (isAppOrder.isChecked()) {
                        isDoubleDisSwitch.setEnabled(true);
                    } else {
                        isDoubleDisSwitch.setEnabled(false);
                    }
                } else {
                    layoutIsBeforeSwitch.setVisibility(View.GONE);
                    isDoubleDisSwitch.setEnabled(false);

                }
                break;

        }
    }

    @OnClick(R.id.btn_save)
    public void onViewClicked() {
        if (!isAppOrder.isChecked()) {
            inputs.remove(disNo);
        }
        if (!isBeforeSwitch.isChecked()) {
            inputs.remove(disNoBefore);
            inputs.remove(timeBefore);
        }
        if (!isAppOrder.isChecked() && !isBeforeSwitch.isChecked()) {
            showProgressDialog();
            mPresenter.updateDiscount(discount.getId(), isAppOrder.isChecked(),
                    isAppOrder.isChecked() ? Integer.valueOf(String.valueOf(Double.valueOf(disNo.getText().toString().trim())*10)) : null,
                    isBeforeSwitch.isChecked(),
                    isBeforeSwitch.isChecked() ? Integer.valueOf(String.valueOf(Double.valueOf(disNoBefore.getText().toString().trim())*10)) : null,
                    isBeforeSwitch.isChecked() ? Integer.valueOf(timeBefore.getText().toString().trim()) : null,
                    isDoubleDisSwitch.isChecked());
        } else {
            if (inputs.test()) {

                showProgressDialog();
                mPresenter.updateDiscount(discount.getId(), isAppOrder.isChecked(),
                        isAppOrder.isChecked() ? Integer.valueOf(String.valueOf(Double.valueOf(disNo.getText().toString().trim())*10)) : null,
                        isBeforeSwitch.isChecked(),
                        isBeforeSwitch.isChecked() ?
                                Integer.valueOf(String.valueOf(Double.valueOf(disNoBefore.getText().toString().trim())*10)) : null,
                        isBeforeSwitch.isChecked() ? Integer.valueOf(timeBefore.getText().toString().trim()) : null,
                        isDoubleDisSwitch.isChecked());
            } else {
                toast("请按要求填写。");
            }
        }

    }

    @Override
    public void upRes(int res) {
        dissProgressDialog();
        if (res == 1) {
            toast(R.string.up_success);
        } else {
            toast(R.string.up_failure);
        }
    }
}
